<?php
/**
 * Created by Silent Night.
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/9/28
 * Time: 9:19
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class Group extends Controller
{
    private $_model;
    private $title_field = [

    ];
    public $pagesize = 10;

    public function _initialize()
    {
        (new \app\admin\behavior\CheckPermissions())->run();//增加权限控制
        $this->_model = new \app\admin\model\Group();
        $this->assign('LoginUserInfo', $this->LoginUserInfo);
        //获取当前用户的权限信息
        $this->assign('ruleLists', $this->LoginUserInfo['CurrPower']);
    }

    /**
     * 列表页
     * @return mixed
     */
    public function index()
    {

        return $this->fetch('index');
    }


    public function recordSystemLog($arr)
    {
        $model = new \app\admin\model\Log();
        $model->add($arr);
    }

    public function lists()
    {

        $ser_key = trim(input("request.ser_key"));//auth
        $ser_val = trim(input("request.ser_val"));//auth
        $type = trim(input("request.type"));//auth


        if ($ser_key && $ser_val) {
            $where[$ser_key] = array('like', '%' . $ser_val . '%');
        }

        if ($type == '0') {
            $where['type'] = $type;
        } else if ($type == '1') {
            $where['type'] = $type;
        }


        $lists = $this->_model->lists($where, $this->pagesize);

        $this->assign('lists', $lists);

        $this->assign('request', input("request."));
        return view('lists');

    }


    /**
     * 删除
     * 支持批量删除和单条删除
     * @return mixed
     */
    public function delData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }

        $oldGroup = $this->_model->getDataForWhere(array(
            'id' => $id
        ));
        if (!empty($oldGroup)) {
            if ($oldGroup['type'] == '0') {
                $return = ['err' => 1, 'msg' => '系统角色，不能够删除！'];
                return json($return);
            }
        }
        $res = $this->_model->delData(array('id' => $id));

        if ($res) {

            //日志
            $action = "删除角色[" . $oldGroup['gname'] . "][id:" . $id . "]";

            $this->recordSystemLog([
                'user' => $this->LoginUserInfo['UserInfo']['id'],
                'time' => date("Y-m-d H:i:s"),
                'action' => $action,
                'type' => 4,
            ]);
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }


    }


    public function addData()
    {
        $time = time();
        $sys_opt = trim(input("request.sys_opt"));//auth
        if ($sys_opt) {
            $gname = trim(input("request.gname"));//auth
            if (empty($gname)) {

                $return = ['err' => 3, 'msg' => '对不起角色名称不能为空'];
                return json($return);
            }
            $oldGroup = $this->_model->getDataForWhere(array(
                'gname' => $gname
            ));
            if (!empty($oldGroup)) {
                $return = ['err' => 3, 'msg' => '该角色已经存在，请不要重复添加'];
                return json($return);
            }

            $insertData = array(
                'gname' => $gname,
                'type' => 1,
                'ctime' => $time,
            );
            $res = $this->_model->add($insertData);

            if ($res) {
                //日志
                $action = "添加角色[" . $insertData['gname'] . "][id:" . $res . "]";

                $this->recordSystemLog([
                    'user' => $this->LoginUserInfo['UserInfo']['id'],
                    'time' => date("Y-m-d H:i:s"),
                    'action' => $action,
                    'type' => 4,
                ]);
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }


        //查询部门
        $depModel = new \app\admin\model\Dep();

        $FirstDepList = $depModel->getDataForWheres(array(
            'father' => 0
        ));//查询一级部门
        $SecondDepList = $depModel->getDataForWheres(array(
            'father' => $FirstDepList[0]['id']
        ));//查询二级部门


        foreach ($SecondDepList as $k => $v) {
            $SecondDepList[$v['id']] = $v;
        }
        foreach ($FirstDepList as $k => $v) {
            $FirstDepList[$v['id']] = $v;
        }


        $this->assign('SecondDepList', $SecondDepList);
        $this->assign('FirstDepList', $FirstDepList);


        //查询分组
        $groupModel = new \app\admin\model\Group();
        $groupList = $groupModel->getGroupList();

        $this->assign('groupList', $groupList);
        //查询用户

        //设置全局信息
        $title = "添加角色";
        $this->assign('title', $title);

        return view('addData');

    }


    public function editData()
    {

        $id = trim(input("request.id"));//auth
        $sys_opt = trim(input("request.sys_opt"));//auth
        if ($sys_opt) {
            $gname = trim(input("request.gname"));//auth


            $oldGroup = $this->_model->getDataForWhere(array(
                'gname' => $gname
            ));

            if (!empty($oldGroup)) {
                if ($oldGroup['id'] != $id) {
                    $return = ['err' => 3, 'msg' => '对不起,该角色名称已经存在'];
                    return json($return);
                }
                if ($oldGroup['type'] == '0') {
                    $return = ['err' => 4, 'msg' => '对不起,系统角色不能够修改'];
                    return json($return);
                }
            }
            $w = array(
                'id' => $id
            );
            $update = array(
                'gname' => $gname,
            );
            $res = $this->_model->updateDataForWhere($update, $w);

            if ($res || ($res === 0)) {
                //日志
                $action = "修改角色[" . $update['gname'] . "][id:" . $res . "]";

                $this->recordSystemLog([
                    'user' => $this->LoginUserInfo['UserInfo']['id'],
                    'time' => date("Y-m-d H:i:s"),
                    'action' => $action,
                    'type' => 4,
                ]);
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }

        //查询用户
        $info = $this->_model->getDataForWhere(array('id' => $id));
        $this->assign('info', $info);
        //查询部门
        $depModel = new \app\admin\model\Dep();

        $FirstDepList = $depModel->getDataForWheres(array(
            'father' => 0
        ));//查询一级部门
        $selectSecondID = $FirstDepList[0]['id'];
        if ($info['first_depid']) {
            $selectSecondID = $info['first_depid'];
        }
        $SecondDepList = $depModel->getDataForWheres(array(
            'father' => $selectSecondID
        ));//查询二级部门
        $this->assign('SecondDepList', $SecondDepList);
        $this->assign('FirstDepList', $FirstDepList);

        //查询分组
        $groupModel = new \app\admin\model\Group();
        $groupList = $groupModel->getGroupList();

        $this->assign('groupList', $groupList);


        //设置全局信息
        $title = "编辑角色";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('editData');

    }


    public function power()
    {

        $id = trim(input("request.id"));//auth
        $sys_opt = trim(input("request.sys_opt"));//auth
        if ($sys_opt) {
            $gname = trim(input("request.gname"));//auth


            $oldGroup = $this->_model->getDataForWhere(array(
                'gname' => $gname
            ));


            $w = array(
                'id' => $id
            );
            $update = array(
                'gname' => $gname,
            );
            $res = $this->_model->updateDataForWhere($update, $w);

            if ($res || ($res === 0)) {
                //日志
                $action = "修改角色[" . $update['gname'] . "][id:" . $res . "]";

                $this->recordSystemLog([
                    'user' => $this->LoginUserInfo['UserInfo']['id'],
                    'time' => date("Y-m-d H:i:s"),
                    'action' => $action,
                    'type' => 4,
                ]);
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }

        //角色信息
        $info = $this->_model->getDataForWhere(array('id' => $id));
        $this->assign('info', $info);


        //查询现有权限信息

        $ruleModel = new \app\admin\model\Rule();
        $ruleLists = $ruleModel->getAll(array('gid' => $id));

        $this->assign('ruleLists', $ruleLists);
        //查询所有权限项

        $ruleobjModel = new \app\admin\model\Ruleobj();
        $ruleobjLists = $ruleobjModel->getAll();

        $this->assign('ruleobjLists', $ruleobjLists);


        //设置全局信息
        $title = "权限设置";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('power');

    }


    public function set_power()
    {
        $id = trim(input("request.id"));//auth
        $power = trim(input("request.power"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }

        $model = new \app\admin\model\Rule();

        $delRes = $model->delData(array('gid' => $id));
        if (!empty($power)) {
            $res = $model->addPower($id, $power);

            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);

        } else {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        }


    }

    public function power1()
    {

        $id = trim(input("request.id"));//auth
        $sys_opt = trim(input("request.sys_opt"));//auth


        //角色信息
        $info = $this->_model->getDataForWhere(array('id' => $id));
        $this->assign('info', $info);


        //查询现有权限信息

        $ruleModel = new \app\admin\model\Rule();
        $ruleLists = $ruleModel->getAll(array('gid' => $id));
        $tempRule = array();
        foreach ($ruleLists as $key => $val) {
            $tempRule[] = $val['rid'];
        }
        $this->assign('tempRule', $tempRule);

        //查询所有权限项

        $ruleobjModel = new \app\admin\model\Ruleobj();
        $ruleobjLists = $ruleobjModel->getAll();

        $this->assign('ruleobjLists', $ruleobjLists);


        //设置全局信息
        $title = "权限设置";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('power1');

    }


    public function power2()
    {

        $id = trim(input("request.id"));//auth
        $sys_opt = trim(input("request.sys_opt"));//auth
        //角色信息
        $info = $this->_model->getDataForWhere(array('id' => $id));
        $this->assign('info', $info);
        //查询现有权限信息
        $ruleModel = new \app\admin\model\Rule();
        $ruleLists = $ruleModel->getAll(array('gid' => $id));
        $tempRule = array();
        foreach ($ruleLists as $key => $val) {
            $tempRule[] = $val['rid'];
        }
        $this->assign('tempRule', $tempRule);

        //查询所有权限项

        $ruleobjModel = new \app\admin\model\Ruleobj();
        $ruleobjLists = $ruleobjModel->getAll1();

        foreach ($ruleobjLists as $key => $val) {

            $ruleobjLists[$key]['ischeck'] = false;
            if (in_array($val['id'], $tempRule)) {
                $ruleobjLists[$key]['ischeck'] = true;
            }
        }

        $this->assign('ruleobjLists', $ruleobjLists);
        //设置全局信息
        $title = "权限设置";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('power2');

    }

}